package com.emin.platform.subjectcenter.api;

import com.emin.base.controller.BaseController;
import com.emin.base.exception.EminException;
import com.emin.platform.subjectcenter.common.response.ResponseBack;
import com.emin.platform.subjectcenter.service.ECMService;
import com.emin.platform.subjectcenter.vo.EcmRequestVo;
import com.emin.platform.subjectcenter.vo.EcmResponseVo;
import com.emin.platform.subjectcenter.vo.PageResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.emin.platform.subjectcenter.exception.ExceptionCode.SYSTEM_ERROR;

/**
 * 主体Controller
 *
 * @author Jack
 * @version V1.0
 * @Title:
 * @Description: 用于主体相关操作
 * @date 2017/10/25 11:31
 */
@Controller
@RequestMapping("/ecm")
public class ECMController extends BaseController {
    @Autowired
    private ECMService ecmService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增或者修改主体信息", tags = {"主体中心基本信息API"}, httpMethod = "POST",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "status", paramType = "form", dataType = "Integer", value =
                    "激活传1，禁用传-1，新增时不用传"),
            @ApiImplicitParam(name = "name", paramType = "form", dataType = "String", value =
                    "主体名称"),
            @ApiImplicitParam(name = "industryIds", paramType = "form", dataType = "String",
                    value =
                            "主体行业ID，多个行业的id之间用逗号！！分割"),
            @ApiImplicitParam(name = "sn", paramType = "form", dataType = "String", value =
                    "主体编号"),
            @ApiImplicitParam(name = "logo", paramType = "form", dataType = "String",
                    value =
                            "主体图标,从上传图片接口的返回值中获取"),
            @ApiImplicitParam(name = "address", paramType = "form", dataType = "String", value =
                    "主体地址"),
            @ApiImplicitParam(name = "describe", paramType = "form", dataType = "String", value =
                    "主体详细描述"),

    })
    public ResponseBack<Boolean> save(/*@RequestBody*/ @RequestParam(value = "ecmStr", required =
            true)
            String ecmStr) {
        try {
            JSONObject json = JSONObject.fromObject(ecmStr);
            EcmRequestVo vo = (EcmRequestVo) JSONObject.toBean(json, EcmRequestVo.class);
            // 数据校验
            ResponseBack<Boolean> checkResult = vo.validate();
            if (null != checkResult) {
                return checkResult;
            }
            return ecmService.save(vo);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/queryPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页查询主体信息", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", paramType = "query", dataType = "Integer", value =
                    "当前页码"),
            @ApiImplicitParam(name = "limit", paramType = "query", dataType = "Integer", value =
                    "每页显示记录数"),
            @ApiImplicitParam(name = "industryId", paramType = "query", dataType = "Long",
                    value =
                            "主体行业ID"),
            @ApiImplicitParam(name = "keyword", paramType = "form", dataType = "String", value =
                    "主体名称或者主体编号，用于模糊查询"),
    })
    public ResponseBack<PageResponse<EcmResponseVo>> queryPage(@RequestParam(value = "industryId",
            required = false
    ) Long industryId, @RequestParam(value = "keyword", required = false) String keyword,
                                                               @RequestParam(value = "page",
                                                                       required = false
                                                               ) Integer
                                                                       page,
                                                               @RequestParam(value = "limit",
                                                                       required = false)
                                                                       Integer
                                                                       limit) {
        try {
            return ecmService.queryPage(getPageRequestData(), industryId, keyword);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/queryDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据ID查询主体详情", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", dataType = "Long", required = true),
    })
    public ResponseBack<EcmResponseVo> queryDetail(@RequestParam(value = "id", required = true)
                                                           Long id) {
        try {
            return ecmService.queryDetail(id);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/activate", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据ID激活主体", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", paramType = "path", dataType = "String", required =
                    true, value = "可传多个，之间以逗号分割"),
    })
    public ResponseBack<Boolean> activate(@RequestParam(value = "ids", required = true) String
                                                  ids) {
        try {
            return ecmService.activate(ids);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/disable", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据ID禁用主体", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", paramType = "path", dataType = "String", required =
                    true, value = "可传多个，之间以逗号分割"),
    })
    public ResponseBack<Boolean> disable(@RequestParam(value = "ids", required = true) String
                                                 ids) {
        try {
            return ecmService.disable(ids);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }


    @RequestMapping(value = "/findByKeyword", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据keyword模糊查询所有主体", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", paramType = "path", dataType = "String", required =
                    true, value = "模糊查询条件"),
    })
    public ResponseBack<List<EcmResponseVo>> findByKeyword(@RequestParam(value = "keyword",
            required = true) String keyword) {
        try {
            return ecmService.findByKeyword(keyword);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/checkIsActivate", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据ID校验主体是否激活", tags = {"主体中心基本信息API"}, httpMethod = "GET",
            produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", dataType = "String", required =
                    true, value = "主体ID"),
    })
    public ResponseBack<Boolean> checkIsActivate(@RequestParam(value = "id", required = true)
                                                         Long id) {
        try {
            return ecmService.checkIsActivate(id);
        } catch (EminException e) {
            return ResponseBack.error(e);
        } catch (Exception e) {
            return ResponseBack.error(SYSTEM_ERROR);
        }
    }


}
